package xin.sunzy.spring.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.MessageSource;
import xin.sunzy.spring.model.PageBean;

import java.util.Locale;

/**
 * @description: 响应工具类
 * @email: <a href="mailto:sunzhenyu@terminus.io">孙振宇</a>
 * @author: ginkgo
 * @date: 2018/4/4
 */
public class ResponseUtils {

    private static MessageSource messageSource;

    public ResponseUtils(MessageSource messageSource) {
        ResponseUtils.messageSource = messageSource;
    }

    public static PageBean ok() {
        return ok(null);
    }

    public static PageBean ok(Object data) {
        return ok(null, data, (Object[]) null);
    }
    public static PageBean ok(String message) {
        return ok(message, (Object[]) null);
    }

    public static PageBean ok(String message, Object... args) {
        return ok(message, null, args);
    }

    public static PageBean ok(String message, Object data, Object... args) {
        return PageBean.ok(convert2Native(message, args), data);
    }

    public static PageBean fail(String message) {
        return fail(message,(Object[])null);
    }
    public static PageBean fail(String message, Object... args) {
        return PageBean.fail(convert2Native(message, args));
    }

    private static String convert2Native(String message, Object... args) {
        String msg = message;
        if (StringUtils.isNotBlank(message)) {
            Locale locale = UserUtils.getCurrentLocale();
            msg = messageSource.getMessage(message, args, message, locale);
        }
        return msg;
    }
}
